/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import controller.ControllerInterface;

/**
 * Opens a JDialog which displays the about info to this application.
 * @author Christian Masus
 *
 */
public class DialogWhatIsMyIP {

	private ControllerInterface controller;
	private final JDialog dialog;
	private JButton okButton;
	private String[][] localIPs;
	private JTextPane[] ipLabels;
	private JLabel[] explanationLabels;
	private JTextPane globalIPLabel;
	private JLabel globalIPExplanationLabel;

	/**
	 * Initiates and displays this JDialog.
	 */
	public DialogWhatIsMyIP(ControllerInterface controller) {
		this.controller = controller;
		JFrame frame = JavaGui.getFrame();
		dialog = new JDialog(frame, "What Is My IP");


		JPanel titlePanel = new JPanel();
		JLabel titleLabel = new JLabel("<html><h1>What Is My IP</h1></html>");
		titlePanel.add(titleLabel);


		Font font = titleLabel.getFont();
		font = font.deriveFont(Font.PLAIN);
		Color backgroundColor = titleLabel.getBackground();
		SimpleAttributeSet attribs = new SimpleAttributeSet();  
		StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT);

		localIPs = controller.getWhatIsMyIP().getLocalIPs();
		ipLabels = new JTextPane[localIPs.length];
		explanationLabels = new JLabel[localIPs.length];
		JPanel ipPanle = new JPanel();
		ipPanle.setBorder(BorderFactory.createEmptyBorder(5,30,5,30));
		if(localIPs != null) {
			ipPanle.setLayout(new GridLayout(localIPs.length+1,2));
			for(int i = 0; i < localIPs.length; i++) {
				ipLabels[i] = new JTextPane();
				ipLabels[i].setText(localIPs[i][0]);
				ipLabels[i].setToolTipText(localIPs[i][1]);
				ipLabels[i].setFont(font);
				ipLabels[i].setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
				ipLabels[i].setEditable(false);
				ipLabels[i].setParagraphAttributes(attribs,true); 
				ipLabels[i].setBackground(backgroundColor);
				explanationLabels[i] = new JLabel(localIPs[i][2]);
				explanationLabels[i].setFont(font);
				explanationLabels[i].setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
				explanationLabels[i].setHorizontalAlignment(SwingConstants.LEFT);
				ipPanle.add(ipLabels[i]);
				ipPanle.add(explanationLabels[i]);
			}
		}
		else {
			ipPanle.setLayout(new GridLayout(1,2));
		}
		globalIPLabel = new JTextPane();
		globalIPLabel.setText("");
		globalIPLabel.setFont(font);
		globalIPLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		globalIPLabel.setEditable(false);
		globalIPLabel.setParagraphAttributes(attribs,true);
		globalIPLabel.setBackground(backgroundColor);
		globalIPExplanationLabel = new JLabel("");
		globalIPExplanationLabel.setFont(font);
		globalIPExplanationLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		globalIPExplanationLabel.setHorizontalAlignment(SwingConstants.LEFT);
		ipPanle.add(globalIPLabel);
		ipPanle.add(globalIPExplanationLabel);


		JPanel buttonPanel = new JPanel();
		okButton = new JButton(closeAction);
		buttonPanel.add(okButton);


		JPanel contentPanel = new JPanel(new BorderLayout());
		contentPanel.add(titlePanel, BorderLayout.NORTH);
		contentPanel.add(ipPanle, BorderLayout.CENTER);
		contentPanel.add(buttonPanel, BorderLayout.SOUTH);
		contentPanel.setOpaque(true);
		dialog.setContentPane(contentPanel);


		dialog.setMinimumSize(new Dimension(350, 100));
		dialog.pack();
		dialog.setResizable(false);
		dialog.setLocationRelativeTo(frame);

		dialog.getRootPane().setDefaultButton(okButton);
		okButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
		okButton.getActionMap().put("close", closeAction);
		okButton.requestFocusInWindow();

		dialog.setVisible(true);

		addGlobalIP();
	}


	/**
	 * Starts a thread that tries the retrieve the Internet address of this computer.
	 */
	private void addGlobalIP() {
		Thread getGlobalIPThread = new Thread(new Runnable() {
			public void run() {
				String[] globalIP = controller.getWhatIsMyIP().getGlobalIP();
				int index = indexOfIPDuplicate(globalIP[0]);
				if(index == -1) {
					globalIPLabel.setText(globalIP[0]);
					globalIPLabel.setToolTipText(globalIP[1]);
					globalIPExplanationLabel.setText(globalIP[2]);
				}
				else {
					ipLabels[index].setText(globalIP[0]);
					ipLabels[index].setToolTipText(globalIP[1]);
					explanationLabels[index].setText(globalIP[2]);
				}
			}
		});
		getGlobalIPThread.start();
	}

	/**
	 * Looks for the index of known IPs. Returns the index if there is a match with the input to this method.
	 * @param globalIPStr the IP, as a string, to look for.
	 * @return the index of the IP or -1 if it is not found.
	 */
	private int indexOfIPDuplicate(String globalIPStr) {
		for(int i = 0; i < localIPs.length; i++) {
			if(localIPs[i][0].equals(globalIPStr)) {
				return i;
			}
		}
		return -1;
	}


	/**
	 * Action for closing this dialog.
	 */
	@SuppressWarnings("serial")
	Action closeAction = new AbstractAction("OK") {
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
			dialog.dispose();
		}
	};

}
